package de.blinkt.openvpn.advancedsettings2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.advancedsettings2.AdvancedSettingsPresenter.NetworkListItem;
import de.blinkt.openvpn.advancedsettings2.ConfigureNetworkItemPresenter.ConfigureNetworkItemFinishedCallback;

public class ConfigureNetworkDialogFragment extends DialogFragment implements ConfigureNetworkItemView {

	private Spinner mProxyType;
	private EditText mProxyServerAddressText;
	private EditText mProxyServerPortText;
	private CheckBox mIndefiniteRetryCheckbox;
	private ConfigureNetworkItemPresenter mPresenter;
	private Button mCancelButton;
	private Button mRemoveNetworkButton;
	private Button mSaveChangesButton;

	
	private int mFragmentId;
	
	public ConfigureNetworkDialogFragment() {

	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.configure_network_properties, container);
		
		mProxyType = (Spinner)v.findViewById(R.id.proxyTypeSelector);
		mProxyServerAddressText = (EditText)v.findViewById(R.id.proxyServerAddressText);
		mProxyServerPortText = (EditText)v.findViewById(R.id.proxyServerPortText);
		mIndefiniteRetryCheckbox = (CheckBox)v.findViewById(R.id.proxyServerIndefiniteRetryCheckbox);
		
		mCancelButton = (Button)v.findViewById(R.id.cancelNetworkChange);
		mRemoveNetworkButton = (Button)v.findViewById(R.id.removeNetworkButton);
		mSaveChangesButton = (Button)v.findViewById(R.id.saveNetworkChanges);
		

		return v;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog d = super.onCreateDialog(savedInstanceState);
		d.setTitle(getActivity().getResources().getString(R.string.configureNetworkDialogTitle));
		ensureKeyboardHidden();
		return d;
	}
	
	public void ensureKeyboardHidden() {
		if(getDialog() != null){
			getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		} else {
			getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		}
	}

	@Override
	public void dismissDialog() {
		getDialog().dismiss();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mPresenter.initView();
	}
	
	@Override
	public Spinner getProxyType() {
		return mProxyType;
	}
	
	@Override
	public EditText getProxyServerAddressText() {
		return mProxyServerAddressText;
	}
	
	@Override
	public EditText getProxyServerPortText() {
		return mProxyServerPortText;
	}
	
	@Override
	public CheckBox getIndefiniteRetryCheckbox() {
		return mIndefiniteRetryCheckbox;
	}
	
	@Override
	public Button getCancelButton() {
		return mCancelButton;
	}
	
	@Override
	public Button getRemoveNetworkButton() {
		return mRemoveNetworkButton;
	}
	@Override
	public Button getSaveChangesButton() {
		return mSaveChangesButton;
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(savedInstanceState != null){
			mPresenter = new ConfigureNetworkItemPresenter();
			mPresenter.setView(this);
			mPresenter.setActivity(getActivity());
			mFragmentId = savedInstanceState.getInt("fragmentId");
		} else {
			//presenter should be provided by creator
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("fragmentId", mFragmentId);
	}

	@Override
	public void setPresenter(ConfigureNetworkItemPresenter presenter) {
		mPresenter = presenter;
	}
	
	@Override
	public Fragment getFragment() {
		return getFragmentManager().findFragmentById(mFragmentId);
	}

	public void setParentFragmentID(int id) {
		mFragmentId = id;
		
	}
	
	private void showAlertDialog(int errorMessageResourceId, int errorTitleResourceId) {
		AlertDialog create = new AlertDialog.Builder(getActivity()).
		setCancelable(false).
		setMessage(errorMessageResourceId).
		setTitle(errorTitleResourceId).
		setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		}).create();
		create.show();
	}

	@Override
	public void showEmptyServerAddressError() {
		showAlertDialog(R.string.invalidProxyServerErrorMessage, R.string.invalidProxyErrorTitle);
	}

	@Override
	public void showInvalidProxyPortError() {
		showAlertDialog(R.string.invalidProxyPortErrorMessage, R.string.invalidProxyErrorTitle);
		
	}

	
}
